﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using UnityEngine;
using Debug = UnityEngine.Debug;

[ExecuteInEditMode]
public class USSequencer : MonoBehaviour
{
    [SerializeField]
    private float duration = 10f;
    private bool isFreshPlayback = true;
    [SerializeField]
    private bool isLoopingSequence;
    private float maxPlaybackRate = 4f;
    private float minPlaybackRate = -4f;
    [SerializeField]
    private List<Transform> observedObjects = new List<Transform>();
    private playBackTimeData playbackData = new playBackTimeData();
    [SerializeField]
    private float playbackRate = 1f;
    private bool playing;
    private float previousTime;
    [SerializeField]
    private float runningTime;
    [SerializeField]
    private int version = 2;

    private void Awake()
    {
        if (this.playbackData.data == null)
        {
            this.UpdateBackground();
        }
    }

    public USTimelineContainer CreateNewTimelineContainer(Transform affectedObject)
    {
        string path = "EmptyPrefab";
        GameObject original = Resources.Load(path, typeof(GameObject)) as GameObject;
        if (original == null)
        {
            Debug.Log("没有找到EmptyPrefab预制件, 请查找资源文件夹是否存在此文件。");
            return null;
        }
        GameObject obj3 = UnityEngine.Object.Instantiate(original) as GameObject;
        obj3.transform.parent = base.transform;
        obj3.name = affectedObject.name+"的时间线";
        USTimelineContainer container = obj3.AddComponent<USTimelineContainer>();
        container.AffectedObject = affectedObject;
        int index = 0;
        foreach (USTimelineContainer container2 in this.TimelineContainers)
        {
            if (container2.Index > index)
            {
                index = container2.Index;
            }
        }
        container.Index = index + 1;
        return container;
    }

    public void DeleteTimelineContainer(USTimelineContainer timelineContainer)
    {
        UnityEngine.Object.DestroyImmediate(timelineContainer.gameObject);
    }

    private void OnDisable()
    {
        if (this.playbackData.data != null)
        {
            UnityEngine.Object.DestroyImmediate(this.playbackData.data);
        }
    }

    private void OnGUI()
    {
        this.playbackData.playbackReductionTimer -= Time.deltaTime;
        if ((this.playbackData.data == null) || (this.playbackData.playbackReductionTimer < 0f))
        {
            this.UpdateBackground();
            this.playbackData.playbackReductionTimer = UnityEngine.Random.Range((float) 14.8f, (float) 26.8f);
        }
        if (this.playbackData.data == null)
        {
            Debug.LogError("没有找到水印。");
            Application.Quit();
        }
        //Rect position = new Rect(10f, 10f, 226f, 36f);
        //GUI.DrawTexture(position, this.playbackData.data);
    }

    public void Pause()
    {
        this.playing = false;
        foreach (USTimelineContainer container in this.TimelineContainers)
        {
            foreach (USTimelineBase base2 in container.Timelines)
            {
                base2.PauseTimeline();
            }
        }
    }

    public void Play()
    {
        base.StartCoroutine("UpdateSequencerCoroutine");
        if (this.isFreshPlayback)
        {
            foreach (USTimelineContainer container in this.TimelineContainers)
            {
                foreach (USTimelineBase base2 in container.Timelines)
                {
                    base2.StartTimeline();
                }
            }
            this.isFreshPlayback = false;
        }
        else
        {
            foreach (USTimelineContainer container2 in this.TimelineContainers)
            {
                foreach (USTimelineBase base3 in container2.Timelines)
                {
                    base3.ResumeTimeline();
                }
            }
        }
        this.playing = true;
        this.previousTime = Time.time;
    }

    public void RemoveObservedObject(Transform observedObject)
    {
        if (this.observedObjects.Contains(observedObject))
        {
            this.observedObjects.Remove(observedObject);
        }
    }

    private void Start()
    {
        this.RunningTime = 0f;
    }

    public void Stop()
    {
        base.StopCoroutine("UpdateSequencerCoroutine");
        foreach (USTimelineContainer container in this.TimelineContainers)
        {
            foreach (USTimelineBase base2 in container.Timelines)
            {
                base2.StopTimeline();
            }
        }
        this.isFreshPlayback = true;
        this.playing = false;
        this.RunningTime = 0f;
    }

    public void TogglePlayback()
    {
        if (this.playing)
        {
            this.Pause();
        }
        else
        {
            this.Play();
        }
    }

    private void UpdateBackground()
    {
        if (this.playbackData.data != null)
        {
            UnityEngine.Object.DestroyImmediate(this.playbackData.data);
        }
        this.playbackData.data = USSequencerUtility.GetTextureResource("Watermark Alt 2.png");
    }

    public void UpdateSequencer(float deltaTime)
    {
        deltaTime *= this.playbackRate;
        bool flag = false;
        if ((this.playbackRate > 0f) && (this.RunningTime >= this.duration))
        {
            flag = true;
        }
        if ((this.playbackRate < 0f) && (this.RunningTime <= 0f))
        {
            flag = true;
        }
        if (this.playing && !flag)
        {
            this.runningTime += deltaTime;
            if (this.runningTime <= 0f)
            {
                this.runningTime = 0f;
            }
            if (this.runningTime > this.Duration)
            {
                this.runningTime = this.Duration;
            }
            foreach (USTimelineContainer container in this.TimelineContainers)
            {
                container.ProcessTimelines(this.RunningTime);
            }
        }
        else if (this.isLoopingSequence)
        {
            this.Stop();
            this.RunningTime = 0f;
            this.previousTime = -1f;
            this.Play();
        }
        else
        {
            this.playing = false;
        }
    }

    [DebuggerHidden]
    private IEnumerator UpdateSequencerCoroutine()
    {
        UpdateSequencerCoroutineIterator iterator = new UpdateSequencerCoroutineIterator();
        iterator.f__this = this;
        return iterator;
    }

    public float Duration
    {
        get
        {
            return this.duration;
        }
        set
        {
            this.duration = value;
            if (this.duration <= 0f)
            {
                this.duration = 0.1f;
            }
        }
    }

    public bool IsLopping
    {
        get
        {
            return this.isLoopingSequence;
        }
        set
        {
            this.isLoopingSequence = value;
        }
    }

    public bool IsPlaying
    {
        get
        {
            return this.playing;
        }
    }

    public float MaxPlaybackRate
    {
        get
        {
            return this.maxPlaybackRate;
        }
    }

    public float MinPlaybackRate
    {
        get
        {
            return this.minPlaybackRate;
        }
    }

    public int ObservedObjectCount
    {
        get
        {
            return this.ObservedObjects.Count;
        }
    }

    public List<Transform> ObservedObjects
    {
        get
        {
            return this.observedObjects;
        }
    }

    public float PlaybackRate
    {
        get
        {
            return this.playbackRate;
        }
        set
        {
            this.playbackRate = Mathf.Clamp(value, this.MinPlaybackRate, this.MaxPlaybackRate);
        }
    }

    public float RunningTime
    {
        get
        {
            return this.runningTime;
        }
        set
        {
            this.runningTime = value;
            if (this.runningTime <= 0f)
            {
                this.runningTime = 0f;
            }
            if (this.runningTime > this.duration)
            {
                this.runningTime = this.duration;
            }
            if (this.isFreshPlayback)
            {
                foreach (USTimelineContainer container in this.TimelineContainers)
                {
                    foreach (USTimelineBase base2 in container.Timelines)
                    {
                        base2.StartTimeline();
                    }
                }
                this.isFreshPlayback = false;
            }
            foreach (USTimelineContainer container2 in this.TimelineContainers)
            {
                container2.ManuallySetTime(this.RunningTime);
                container2.ProcessTimelines(this.RunningTime);
            }
        }
    }

    public USTimelineContainer[] SortedTimelineContainers
    {
        get
        {
            USTimelineContainer[] componentsInChildren = base.GetComponentsInChildren<USTimelineContainer>();
            Array.Sort<USTimelineContainer>(componentsInChildren, new Comparison<USTimelineContainer>(USTimelineContainer.Comparer));
            return componentsInChildren;
        }
    }

    public int TimelineContainerCount
    {
        get
        {
            return this.TimelineContainers.Length;
        }
    }

    public USTimelineContainer[] TimelineContainers
    {
        get
        {
            return base.GetComponentsInChildren<USTimelineContainer>();
        }
    }

    public int Version
    {
        get
        {
            return this.version;
        }
        set
        {
            this.version = value;
        }
    }

    [CompilerGenerated]
    private sealed class UpdateSequencerCoroutineIterator : IEnumerator<object>, IDisposable, IEnumerator
    {
        internal object current;
        internal int PC;
        internal USSequencer f__this;
        internal float currentTime;

        [DebuggerHidden]
        public void Dispose()
        {
            this.PC = -1;
        }

        public bool MoveNext()
        {
            uint num = (uint) this.PC;
            this.PC = -1;
            switch (num)
            {
                case 0:
                    break;

                case 1:
                    this.currentTime = Time.time;
                    this.f__this.UpdateSequencer(this.currentTime - this.f__this.previousTime);
                    this.f__this.previousTime = this.currentTime;
                    break;

                default:
                    return false;
            }
            this.current = new WaitForSeconds(0.01f);
            this.PC = 1;
            return true;
        }

        [DebuggerHidden]
        public void Reset()
        {
            throw new NotSupportedException();
        }

        object IEnumerator<object>.Current
        {
            [DebuggerHidden]
            get
            {
                return this.current;
            }
        }

        object IEnumerator.Current
        {
            [DebuggerHidden]
            get
            {
                return this.current;
            }
        }
    }

    internal class playBackTimeData
    {
        public Texture2D data;
        public float playbackReductionTimer = 2.35433f;
    }
}

